Get a string made of the first/last 2 chars¶
Get a string made of the first 2 and the last 2 chars from a given string.
If the string length is less than 2, return instead of the empty string.
Sample String :
‘w3resource’
Expected Result :
‘w3ce’
Sample String :
‘w3’
Expected Result :
‘w3w3’
Sample String :
‘ w’
Expected Result :
Empty String
def string_both_ends(S):
if len(S) < 2:
return ''
return S[0:2] + S[-2:]
# test
print(string_both_ends('w3resource')) # w3ce
print(string_both_ends('w3')) # w3w3
print(string_both_ends('w')) #